home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 524 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.1 KB

  1. From: gnb@bby.com.au (Gregory Bond)
  2. Message-ID: <GNB.96Feb19132020@dame.bby.com.au>
  3. X-Original-Date: 19 Feb 1996 02:20:19 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 19 Feb 96 02:41:02 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Extern "C" {} & class forward declarations
  9. Organization: Burdett, Buckeridge & Young Ltd., Melbourne, Australia
  10. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  11.     iQBFAgUBMSfjaOEDnX0m9pzZAQGEyAF9FSMp7REwwPh74F47MXnRmkxDYbccEQZT
  12.     t557wSLNjEoG2lxbZJitOe/74oQYaddZ
  13.     =puoS
  14.  
  15. I'm trying to write a C++ wrapper around a C struct+functions that is
  16. more-or-less an object.  But I am having problems that seem to me to
  17. have no standard-conforming solution.
  18.  
  19. Because the C header file is large and full of lots of irrelevent
  20. details I am trying to hide, the wrapper class has a pointer to the C
  21. struct and the C struct is forward-declared.  The implementation of
  22. the object naturally has to include the C header.
  23.  
  24. I can find (in the jan96 dwp) no notion of how linkage-specs's apply
  25. to _types_ - 7.5 [dcl.link] only explicitly refers to _objects_ and
  26. _functions_.
  27.  
  28. To concretise the discussion:
  29. -- thing.h --
  30. /* A C header */
  31. typedef struct {} thing;
  32. /* Bunch of functions that operate on thing *'s */
  33. -- 
  34. -- Thing.H --
  35. // C++ Wrapper object
  36. class thing;
  37. class Thing {
  38. private:
  39.     thing *_o;
  40. public:
  41. // bunch of member functions
  42. };
  43. --
  44.  
  45. Now, how do I implement Thing.C in a conforming manner?  If I do
  46. this:
  47. -- Thing.C --
  48. #include "Thing.H"
  49. extern "C" {
  50. #include "thing.h"
  51. }
  52. --
  53. then the compiler complains that "thing" is redeclared - once with
  54. (implicit) C++ linkage, once with C linkage.  This is as it should be
  55. if "thing" were an object or function: see dwp 7.5.3.
  56.  
  57. If I do this:
  58. -- Thing.C --
  59. extern "C" {
  60. #include "thing.h"
  61. }
  62. #include "Thing.H"
  63. --
  64. then it compiles & runs OK, but by the one-definition-rule we have a
  65. non-conforming program because the linkage of the type "thing" is
  66. different in Thing.C to the rest of the program.
  67.  
  68. I tried wrapping the forward-dec of thing in the Thing.H header in
  69. extern "C" {}, but my compiler (g++ 2.7.2) seems to ignore the linkage
  70. of forward-declared classes (which seems reasonable) and I get the
  71. same double-definition error.
  72.  
  73. So it seems I am stuck.  I can include the full C header in the Thing.H
  74. header, which exposes all the stuff I was trying to hide.  Or I grin
  75. and bear it and live with violating the ODR and hope it never jumps up
  76. and bites me.
  77.  
  78. Or the standard adds some sort of caveat to either the link spec or ODR to
  79. explicitly make one or other of these solutions legal.....
  80.  
  81. Or (of couse) I missed something!
  82.  
  83. Greg.
  84. --
  85. Gregory Bond <gnb@bby.com.au> Burdett Buckeridge & Young Ltd Melbourne Australi
  86. a
  87. ``Efforts to maintain the "purity" of a language only succeed in establishing
  88.   an elite class of people who know the shibboleths.  Ordinary folks know
  89.   better, even if they don't know what "shibboleth" means.'' - Larry Wall
  90. ---
  91. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  92.   Contact address: std-c++-request@ncar.ucar.edu.  Moderation policy:
  93.   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  94.